home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Russian
/
cpsimage.cab
/
data
/
xps
/
xmetWriteXML.proc
< prev
next >
Wrap
Text File
|
2009-04-23
|
17KB
|
473 lines
#load "xipProcs/xipflatten.proc";
#load "sys/transform2d.elf";
/* @WriteContentTypes
Support procedure to write XPS XML support files
Takes N-layer image and filename (no path or extension) and creates
four XML support files;
page1.xaml,
page1.xaml.rels,
page1.xaml,
.rels,
*/
private
PROCEDURE WriteContentTypes (STRING dir, INTEGER page)
{
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">\n" +
"<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>\n" +
"<Default Extension=\"fdseq\" ContentType=\"application/vnd.ms-package.xps-fixeddocumentsequence+xml\"/>\n" +
"<Default Extension=\"xml\" ContentType=\"application/xml\"/>\n" +
"<Default Extension=\"fdoc\" ContentType=\"application/vnd.ms-package.xps-fixeddocument+xml\"/>\n" +
"<Default Extension=\"fpage\" ContentType=\"application/vnd.ms-package.xps-fixedpage+xml\"/>\n" +
"<Default Extension=\"odttf\" ContentType=\"application/vnd.ms-package.obfuscated-opentype\"/>\n" +
"<Default Extension=\"ttf\" ContentType=\"application/vnd.ms-opentype\"/>\n" +
"<Default Extension=\"jpg\" ContentType=\"image/jpeg\"/>\n" +
"<Default Extension=\"png\" ContentType=\"image/png\"/>\n" +
"<Default Extension=\"tif\" ContentType=\"image/tiff\"/>\n" +
"<Override PartName=\"/core.xml\" ContentType=\"application/vnd.openxmlformats-package.core-properties+xml\"/>\n" +
"</Types>\n";
str.Write (filename: dir + "/[Content_Types].xml", value: 1);
}
private
PROCEDURE WriteDocSeq (STRING dir)
{
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<FixedDocumentSequence xmlns=\"http://schemas.microsoft.com/xps/2005/06\">\n" +
"<DocumentReference Source=\"/FixedDocument.fdoc\"/>\n" +
"</FixedDocumentSequence>\n";
str.Write (filename: dir + "/FixedDocumentSequence.fdseq", value:1);
}
private
PROCEDURE WriteFixedDocHdr (STRING dir, INTEGER page)
{
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<FixedDocument xmlns=\"http://schemas.microsoft.com/xps/2005/06\">\n";
str.Write (filename: dir + "/FixedDocument.fdoc", value:1);
}
private
PROCEDURE WriteFixedDoc (STRING dir, INTEGER page)
{
STRING str =
"<PageContent Source=\"/" + page + ".fpage\"/>\n";
str.Write (filename: dir + "/FixedDocument.fdoc", mode:"a", value:1);
}
private
PROCEDURE WriteFixedDocEnd (STRING dir)
{
STRING str = "</FixedDocument>\n";
str.Write (filename: dir + "/FixedDocument.fdoc", mode:"a", value:1);
}
private
PROCEDURE mapHex (INTEGER num)
RETURNS (STRING hex)
{
STRING h = "0123456789ABCDEF";
INTEGER v1 = num / 16;
INTEGER v2 = num - v1 * 16;
hex = h.index (val:v1) + h.index (val:v2);
}
private
PROCEDURE mapExt (XIPIMAGE img, INTEGER layer, INTEGER isTIFF)
RETURNS (STRING ext)
{
ext = "unknown";
INTEGER ltype = img.getMember (num:layer, member:"layerType");
INTEGER comp = img.getMember (num:layer, member:"compression");
if ( ltype == XIP_Contone || comp == XIP_JPEG_COMP) {
ext = "jpg";
} else {
ext = "png";
if (isTIFF && ltype != XIP_ColorMask) ext = "tif";
}
}
private
PROCEDURE makeViewboxValue (XIPIMAGE img, INTEGER layerNum)
RETURNS (STRING value) {
DOUBLE xres, yres;
DOUBLE pix = img.getMember (num:layerNum, member:"pixels");
DOUBLE scl = img.getMember (num:layerNum, member:"scanlines");
xres = img.getMember (num:layerNum, member:"xres");
yres = img.getMember (num:layerNum, member:"yres");
if (xres == 0.0)
xres = 96.0;
if (yres == 0.0)
yres = 96.0;
DOUBLE w = pix * 96.0 / xres;
DOUBLE h = scl * 96.0 / yres;
value = "\"0,0," + w + "," + h + "\"";
}
PROCEDURE makeViewportValue (LIST win) RETURNS (STRING value)
{
value = "\"" + win[0] + "," + win[1] + "," + win[2] + "," + win[3] + "\"";
}
PROCEDURE makePathDataValue (LIST viewport) RETURNS (STRING value)
{
DOUBLE x, y, w, h;
x = viewport[0];
y = viewport[1];
w = viewport[2];
h = viewport[3];
value = x + "," + y + " L " +
(x+w) + "," + y + " " +
(x+w) + "," + (y+h) + " " +
x + "," + (y+h);
}
/* Arguments are:
** The Page Image
** The file name we're writing to
** The bounding box X position
** The bounding box Y position
** spi / resolution x
** spi / resolution y
** The page number
** OCR data if we have it
** Boolean for are we writing TIFF files
*/
private
PROCEDURE
WriteFixedPgLayers (XIPIMAGE img, STRING file,
DOUBLE bx, DOUBLE by,
DOUBLE fx, DOUBLE fy,
INTEGER page, STRING ocrxml, INTEGER isTIFF)
{
INTEGER ltype, lcomp, i, irot;
DOUBLE xres, yres;
DOUBLE ww=img.imageWidth, hh=img.imageHeight, w, h, xpos, ypos, rot, cos, sin;
STRING brush, ext, str, stroke; // ="Stroke=\"#0000ff\"" // debugging;
STRING xform_str, path_data_str, viewbox_str, viewport_str;
XIPCOLOR xipcolor;
LIST xform, viewport, window;
TRANSFORM2D matrix, rotmatrix;
/*
** Get orthogonal page rotation. Note that XPS uses CW rotation for
** positive angles. Our document model defines CCW direction for the
** rotation of positive angles. To conform to the document model, the
** orthogonal page rotation angle must be negated.
*/
rot = img.getAttr (name:"_PDFrotate");
rot = -1 * rot;
if (rot != 0) {
window = (0.0, 0.0, img.imageWidth, img.imageHeight);
rotmatrix.translate (tx: -1 * window[2] / 2.0, ty: -1 * window[3] / 2.0);
rotmatrix.rotate (angle: rot);
rotmatrix.applyToWin (window: window);
rotmatrix.translate (tx: window[2] / 2.0, ty: window[3] / 2.0);
}
for ( i=0; i<img.nlayers; i++ )
{
ltype = img.getMember (num:i, member:"layerType");
if ( ltype == XIP_JBIG2Dict || ltype == XIP_Thumbnail)
continue;
xpos = bx + img.getMember (num:i, member:"xpos") * fx;
ypos = by + img.getMember (num:i, member:"ypos") * fy;
w = img.getMember (num:i, member:"width") * fx;
h = img.getMember (num:i, member:"height") * fy;
rot = img.getMember (num:i, member:"rotation");
irot = rot;
cos = rot.cos();
sin = rot.sin();
ext = mapExt (img:img, layer:i, isTIFF: isTIFF);
/* this clears very small numbers */
if (cos.abs() < .00001) cos = 0;
if (sin.abs() < .00001) sin = 0;
// Create viewbox value for current layer
viewbox_str = makeViewboxValue (img: img, layerNum: i);
// Create matrix transform value
xform = img.getAttr (num:i, name:"_DIOMatrixTransform");
if (xform) {
xform[4] *= fx;
xform[5] *= fy;
}
else if ( rot != 0 && ltype == XIP_Text) {
// temporary hack until transforms are fixed for Text layers
xform = (cos, -sin, sin, cos, xpos, ypos);
}
else {
xform = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
}
matrix.setCoeffs (coeffs: xform);
matrix.concatenate (other: rotmatrix);
xform_str = "\"" + matrix.toString () + "\"";
viewport = (xpos, ypos, w, h);
matrix.invert ();
viewport = matrix.applyToWin (window: viewport);
viewport_str = makeViewportValue (win: viewport);
path_data_str = makePathDataValue (viewport: viewport);
if ( ltype == XIP_Contone || ltype == XIP_Binary )
{
str = "<Canvas>\n" +
" <Canvas.RenderTransform>\n" +
" <MatrixTransform Matrix=" + xform_str +"/>\n" +
" </Canvas.RenderTransform>\n" +
" <Path " + stroke + " Data=\"M "+ path_data_str +" Z\">\n" +
" <Path.Fill>\n" +
" <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
" ViewboxUnits=\"Absolute\"\n" +
" Viewbox=" + viewbox_str + "\n"+
" ViewportUnits=\"Absolute\"\n" +
" Viewport=" + viewport_str +"/>\n" +
" </Path.Fill>\n" +
" </Path>\n" +
"</Canvas>\n";
}
else if ( ltype == XIP_Text )
{
STRING vis = "\"0.0\"";
if ( "$ESIPS_TEST_ENV" ) vis = "\"1.0\"";
str = "<Canvas" + " Opacity = " + vis + " RenderTransform=" + xform_str + ">\n";
str.Write (filename: file, value:1, mode:"a");
ocrxml.Write (filename: file, value:1, mode:"a");
str = "</Canvas>\n";
}
else if ( ltype == XIP_ColorMask )
{
xipcolor = img.getMember(num:i, member:"color");
if ( xipcolor ) {
if ((xipcolor.photometry == XIP_SGRAY_COLOR) || (xipcolor.photometry == XIP_GRAY_COLOR))
brush = "#" + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[0]);
else
brush = "#" + mapHex(num: xipcolor.c[0]) + mapHex(num: xipcolor.c[1]) + mapHex(num: xipcolor.c[2]);
}
else
brush = "#000000";
str = "<Canvas>\n" +
" <Canvas.RenderTransform>\n" +
" <MatrixTransform Matrix=" + xform_str + "/>\n" +
" </Canvas.RenderTransform>\n" +
" <Path " + stroke + " Fill=\""+brush+"\" "+"Data=\"M "+ path_data_str +" Z\">\n" +
" <Path.OpacityMask>\n" +
" <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
" ViewboxUnits=\"Absolute\"\n" +
" Viewbox=" + viewbox_str + "\n"+
" ViewportUnits=\"Absolute\"\n" +
" Viewport=" + viewport_str + "/>\n" +
" </Path.OpacityMask>\n" +
" </Path>\n" +
"</Canvas>\n";
}
else if ( ltype == XIP_ContoneMask )
{
str = "<Canvas RenderOptions.EdgeMode=\"Aliased\">\n" +
" <Canvas.RenderTransform>\n" +
" <MatrixTransform Matrix=" + xform_str +"/>\n" +
" </Canvas.RenderTransform>\n" +
" <Path " + stroke + " Data=\"M "+ path_data_str +" Z\">\n" +
" <Path.OpacityMask>\n" +
" <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
" ViewboxUnits=\"Absolute\"\n" +
" Viewbox="+ viewbox_str + "\n"+
" ViewportUnits=\"Absolute\"\n" +
" Viewport=" + viewport_str + "/>\n" +
" </Path.OpacityMask>\n";
i++; // Get the next Layer
xpos = bx + img.getMember (num:i, member:"xpos") * fx;
ypos = by + img.getMember (num:i, member:"ypos") * fy;
w = img.getMember (num:i, member:"width") * fx;
h = img.getMember (num:i, member:"height") * fy;
ext = mapExt (img:img, layer:i, isTIFF: isTIFF);
// Create viewbox value for next layer
viewbox_str = makeViewboxValue (img: img, layerNum: i);
viewport = (xpos, ypos, w, h);
viewport = matrix.applyToWin (window: viewport);
viewport_str = makeViewportValue (win: viewport);
str = str +
" <Path.Fill>\n" +
" <ImageBrush ImageSource=\"/Images/pg"+page+"Layer"+i+"."+ext+"\"\n" +
" ViewboxUnits=\"Absolute\"\n" +
" Viewbox=" + viewbox_str + "\n"+
" ViewportUnits=\"Absolute\"\n" +
" Viewport=" + viewport_str + "/>\n" +
" </Path.Fill>\n" +
" </Path>\n" +
"</Canvas>\n";
}
str.Write (filename: file, value:1, mode:"a");
}
}
private
PROCEDURE WriteFixedPg (XIPIMAGE img, STRING dir, INTEGER page, STRING ocrxml,
DOUBLE page_x, DOUBLE page_y, INTEGER unit, INTEGER isTIFF)
{
DOUBLE fx = 96 / img.resolution[0];
DOUBLE fy = 96 / img.resolution[1];
DOUBLE w = img.imageWidth * fx;
DOUBLE h = img.imageHeight * fy;
DOUBLE pw = w; // Page width
DOUBLE ph = h; // Page height
// Base unit is spots per inch
if ( unit ) {
pw = 96 * page_x;
ph = 96 * page_y;
if ( unit == 2 ) { // millimeters
pw = pw / 25.4;
ph = ph / 25.4;
}
}
STRING file = dir + "/" + page + ".fpage";
STRING ext = mapExt (img:img, layer:0, isTIFF: isTIFF);
STRING str =
"<FixedPage " + "Width=\"" + pw + "\" " + "Height=\"" + ph + "\" " +
"xmlns=\"http://schemas.microsoft.com/xps/2005/06\" " +
"xml:lang=\"en-US\">\n";
str.Write (filename: file, value:1);
DOUBLE bx = pw/2 - w/2; // Bounding box X edge
DOUBLE by = ph/2 - h/2; // Bounding box Y edge
WriteFixedPgLayers (img:img, file:file, bx:bx, by:by, fx:fx, fy:fy, page:page, ocrxml:ocrxml, isTIFF: isTIFF);
str = "</FixedPage>\n";
str.Write (filename: file, value:1, mode:"a");
}
private
PROCEDURE WriteRelsFixedPg (XIPIMAGE img, STRING dir, INTEGER page, STRING thumbName, STRING font, INTEGER isTIFF)
{
STRING filename = dir + "/" + page + ".fpage.rels";
INTEGER i, ltype;
INTEGER tid = -1;
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n";
str.Write (filename: filename, value:1);
for ( i=0; i<img.nlayers; i++ ) {
ltype = img.getMember (num:i, member:"layerType");
if ( ltype == XIP_JBIG2Dict) {
continue;
} else if ( ltype == XIP_Thumbnail) {
// remember the layer index. Thumbnail should be written at the end
tid = i;
} else if ( ltype == XIP_Text) {
if (!font)
continue;
str = "<Relationship Target=\"/Fonts/" + font +
"\" Id=\"R"+ i +"\" " +
"Type=\"http://schemas.microsoft.com/xps/2005/06/required-resource\"/>\n";
str.Write (filename: filename, value:1, mode:"a");
} else {
str = "<Relationship Target=\"/Images/pg"+page+"Layer" + i + "." +
mapExt (img:img, layer:i, isTIFF: isTIFF) + "\" Id=\"R"+ i +"\" " +
"Type=\"http://schemas.microsoft.com/xps/2005/06/required-resource\"/>\n";
str.Write (filename: filename, value:1, mode:"a");
}
}
if (tid >=0) {
str = "<Relationship Target=\"" + thumbName + "\" Id=\"R"+ i + "\" " +
"Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail\"/>\n";
str.Write (filename: filename, value:1, mode:"a");
}
str = "</Relationships>\n";
str.Write (filename: filename, value:1, mode:"a");
}
private
PROCEDURE WriteRelsDotRels (STRING dir, STRING thumbName)
{
STRING fname = "FixedDocumentSequence.fdseq";
STRING filename = dir + "/.rels";
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n" +
"<Relationship Target=\"/" + fname + "\" Id=\"R0\" Type=\"" +
"http://schemas.microsoft.com/xps/2005/06/fixedrepresentation\"/>\n" +
"<Relationship Target=\"/core.xml\" Id=\"R1\" " +
"Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\"/>\n";
str.Write (filename: filename, value:1);
if (thumbName != "") {
str = "<Relationship Target=\"" + thumbName + "\" Id=\"R2\" " +
"Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail\"/>\n";
str.Write (filename: filename, value:1, mode:"a");
}
str = "</Relationships>\n";
str.Write (filename: filename, value:1, mode:"a");
}
private
PROCEDURE WriteDocSeqRels (STRING dir)
{
STRING str =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">\n" +
"</Relationships>\n";
str.Write (filename: dir + "/FixedDocumentSequence.fdseq.rels", value:1);
}
private
PROCEDURE WriteMetaData (STRING dir, STRING creator, STRING title, STRING keyword, STRING dateTime)
{
STRING str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<coreProperties xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " +
"xmlns:dcterms=\"http://purl.org/dc/terms/\" " +
"xmlns=\"http://schemas.openxmlformats.org/package/2006/metadata/core-properties\" " +
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> \n" +
"<dc:title>" + title + "</dc:title><dc:subject></dc:subject><dc:creator>" + creator +
"</dc:creator><dcterms:created xsi:type=\"dcterms:W3CDTF\">" + dateTime +
"</dcterms:created><dcterms:modified xsi:type=\"dcterms:W3CDTF\">" + dateTime +
"</dcterms:modified><keywords>" + keyword + "</keywords></coreProperties>\n";
str.Write (filename: dir + "/core.xml", value:1);
}